package com.sztz.utils;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.AsymmetricAlgorithm;
import cn.hutool.json.JSONObject;
import io.jsonwebtoken.*;
import lombok.extern.slf4j.Slf4j;

import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Base64;
import java.util.Date;

/**
 * jwt令牌工具类
 * @author yzy
 * @date 2022/9/20 14:01
 */
@Slf4j
public class JJwtUtils {
    /**
     * RSA加密私钥(模数)长度必须>=2048位
     */
    private static final String PRI_RSA_KEY = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAICw4e9Q7CevqQ1KG+x/jQUbNB7RSAe25nBGBzIF90WnKOob4M6WA8WHuSlSudp/Vo74sdL2aKXhakgAQYZ/lx98SIvTDXB3o48V3YFakKHXJrluF0YkY3Wtghgy38BVPHz0Y4lZgh0ISlklECOK3ZmB2nroyxHK34tw2Y0pv9xpAgMBAAECgYBjj3Tb/tEUyvOQknsT9oIVJzylcue1AC07QxIiAzeQLYibxcGgUbLNSPlZHOUoQ0emCIUaXwQLAV+GIJyQ/3jcC6RCZFMX+GbQ/gwzAgztGjRTbqX5AkkjQIjC2ideStSC/B+fKi3yKwUy4SKrTEbunZohLs2wAkWMcRerVLXjmQJBAMCpFjaLOvVk8QyhrcWz4z6cVglYC5Ej9JHZi0xephuFtsJjYq1dVoX0IZC1c/teoCW099ZcNqrOfr41AH+wlJcCQQCq/+rec9446EfVUqSkyZpBuqOvGmrfqRqP8HkX2mtAqFCB552w6hz0gq4Zw5080hkdvFrycpP+XGi1psdvHDb/AkBvDQfQyNBjQpvvPAJ+QwJpxJOTLzz7nE4NA+hXTPdSRfDie4cVhweojuAKGY2RByookfeJh/261uwXl1WCRLZhAkEAiAFGMCoeHEr7Hzs9Amfqjc7Bc3rieAPKwaM03Pj3BxUX+0pZzKVX5E2v5YQFIqPEIDd4ao8hEFU1Xw1IYCIjxwJARIo0j1GB67kM4WIZdTHY0owOopzFIPJQpdRotHzAYYMsyH51++8LEMVvwwpWvLw3KbjJIrPyfl5NH9bAum8UWA==";
    /**
     * 生成jwt串
     * @author yzy
     * @date 2022/9/20 14:01
     */
    public static String createJwtStr(){
        //使用RSA 非对称加密算法
        SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.RS512;
        //RSA私钥转换
        PrivateKey privateKey = SecureUtil.generatePrivateKey(AsymmetricAlgorithm.RSA.getValue(), Base64.getDecoder().decode(PRI_RSA_KEY));
        //使用RSA私钥生成jwt串
        JwtBuilder builder = Jwts.builder().signWith(signatureAlgorithm,privateKey);
        //设置过期时间为3分钟
        builder.setExpiration(new Date(System.currentTimeMillis()+600*60*1000));
        //需要生成jwt串的内容
        JSONObject userInfo = new JSONObject();
        userInfo.put("userId","QHYZYT0");
        userInfo.put("entName","深圳市前海智慧园区有限公司");
        userInfo.put("scCode","91440300MA5EU8KU7W");
        userInfo.put("userName","");
        userInfo.put("mobile","");
        userInfo.put("cusCode","");
        builder.addClaims(userInfo);
        return builder.compact();
    }
    /**
     * RSA解密公钥(模数)长度必须>=2048位
     */
    private static final String PUB_RSA_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCAsOHvUOwnr6kNShvsf40FGzQe0UgHtuZwRgcyBfdFpyjqG+DOlgPFh7kpUrnaf1aO+LHS9mil4WpIAEGGf5cffEiL0w1wd6OPFd2BWpCh1ya5bhdGJGN1rYIYMt/AVTx89GOJWYIdCEpZJRAjit2Zgdp66MsRyt+LcNmNKb/caQIDAQAB";
    /**
     * 解析jwt串
     * @author yzy
     * @date 2022/9/20 14:01
     */
    public static JSONObject verifyJwt(String jwt){
        PublicKey publicKey = SecureUtil.generatePublicKey(AsymmetricAlgorithm.RSA.getValue(),Base64.getDecoder().decode(PUB_RSA_KEY));
        Jws<Claims> claimsJws = Jwts.parser().setSigningKey(publicKey).parseClaimsJws(jwt);
        Claims body = claimsJws.getBody();
        JSONObject obj =  new JSONObject();
        obj.putAll(body);
        return obj;
    }

    public static void main(String[] args) {
        log.info(createJwtStr());
    }
}
